﻿using FinalPointSet.Model.Model;
using FinalPointSet.Model.Util;
using FinalPointSet.View;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Windows;
using System.Windows.Input;
using System.Windows.Interop;
using static FinalPointSet.Model.Util.HotKeyUtil;

namespace FinalPointSet.Model.Manager
{
    public class HotKeyManager : SingletonBase<HotKeyManager>
    {
        /// <summary>
        /// 搜索页面名称
        /// </summary>
        public const string SearchView = "SearchView";
        /// <summary>
        /// 主页面名称
        /// </summary>
        public const string MainView = "MainView";
        public List<HotKeyCombination> HoyKeyCombinations = new();
        public Dictionary<string, HotKeyCallBackHanlder> callBacks = new();
        public HotKeyManager()
        {
            HoyKeyCombinations = AssetManager.Instance.GetJsonObjectList<HotKeyCombination>();
            //初始化按键键值
            UpdateHotKeyCombination();

            //添加回调函数
            callBacks.Add(SearchView, () => WindowUtil.Instance.Show(typeof(SearchView), Location.Center));
            callBacks.Add(MainView, () => WindowUtil.Instance.Show(typeof(MainView), Location.Center, ScaleMode.Auto));
        }
        public HotKeyCombination GetHotKeyCombination(string name)
        {
            return HoyKeyCombinations.FirstOrDefault(k => k.Name.Equals(name), new HotKeyCombination { Name = name });
        }

        /// <summary>
        /// 注册快捷键
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public bool RegistHotkey(string name)
        {
            Debug.Print($"HotKeyManager.RegistHotkey, name={name}");
            HotKeyCombination hotkeyCombin = GetHotKeyCombination(name);
            HotKey normalKey = hotkeyCombin.NormalKey;
            List<HotKey> specialKeys = hotkeyCombin.SpecialKeys;
            //只有普通按键则直接退出
            if (normalKey.Key == Key.None) return false;

            HotkeyModifiers hotkeyModifiers = HotkeyModifiers.None;
            if (specialKeys.Any(e => HotKeyUtil.IsCtrl(e.Key)))
            {
                hotkeyModifiers |= HotkeyModifiers.Ctrl;
            }
            if (specialKeys.Any(e => HotKeyUtil.IsAlt(e.Key)))
            {
                hotkeyModifiers |= HotkeyModifiers.Alt;
            }
            if (specialKeys.Any(e => HotKeyUtil.IsShift(e.Key)))
            {
                hotkeyModifiers |= HotkeyModifiers.Shift;
            }

            Window? window = WindowUtil.Instance.Get(typeof(StartView));
            if (window == null)
            {
                Debug.Print($"HotKeyManager.RegistHotkey, name={name}, window为空");
                return false;
            }
            HotKeyCallBackHanlder callback = callBacks.GetValueOrDefault(name, () => { });

            bool result = HotKeyUtil.Regist(window, hotkeyModifiers, normalKey.Key, callback);
            return result;
        }

        /// <summary>
        /// 注销快捷键
        /// </summary>
        /// <param name="name"></param>
        public void UnRegistHotKey(string name)
        {
            Debug.Print($"HotKeyManager.UnRegistHotKey, name={name}");
            Window? window = WindowUtil.Instance.Get(typeof(StartView));
            HotKeyCallBackHanlder callback = callBacks.GetValueOrDefault(name, () => { });
            HotKeyUtil.UnRegist(new WindowInteropHelper(window).Handle, callback);
        }

        /// <summary>
        /// 更新快捷键组合的状态
        /// </summary>
        public void UpdateHotKeyCombination()
        {
            //初始化按键键值
            foreach (HotKeyCombination item in HoyKeyCombinations)
            {
                int count = 0;
                item.NormalKey.Key = HotKeyUtil.GetKeyCode(item.NormalKey.Name);
                if (item.NormalKey.Key != Key.None)
                {
                    count++;
                }
                foreach (HotKey hotKey in item.SpecialKeys)
                {
                    hotKey.Key = HotKeyUtil.GetKeyCode(hotKey.Name);
                    if (hotKey.Key != Key.None)
                    {
                        count++;
                    }
                }
                item.Count = count;
            }
        }

        /// <summary>
        /// 保存快捷键设置
        /// </summary>
        public void Save()
        {
            AssetManager.Instance.SaveJsonObjectList<HotKeyCombination>();
        }
    }

}
